package cn.chenshiwu121.controller;

import cn.chenshiwu121.pojo.EasyUIDataGridResult;
import cn.chenshiwu121.pojo.Item;
import cn.chenshiwu121.pojo.TaotaoResult;
import cn.chenshiwu121.service.ItemService;
import cn.chenshiwu121.utils.FastDFSClient;
import cn.chenshiwu121.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
public class ItemController {

    @Value("${TAOTAO_IMAGE_SERVER_URL}")
    private String TAOTAO_IMAGE_SERVER_URL;

    @Autowired
    private ItemService itemService;

    @RequestMapping(value = "/item/list", method = RequestMethod.GET)
    @ResponseBody
    public EasyUIDataGridResult<Item> itemList(Integer page, Integer rows) {
        return itemService.getItemList(page, rows);
    }

    @RequestMapping(value = "/item/save", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult save(Item item) {
        TaotaoResult result = null;
        try {
            itemService.save(item);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/item/update", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult updateItemById(Item item) {
        TaotaoResult result = null;
        try {
            itemService.updateItemById(item);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/item/delete", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult removeItemById(String ids) {
        TaotaoResult result = null;
        try {
            itemService.removeItemById(ids);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/item/instock", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult instock(String ids) {
        TaotaoResult result = null;
        try {
            itemService.instockItemById(ids);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/item/reshelf", method = RequestMethod.POST)
    @ResponseBody
    public TaotaoResult reshelf(String ids) {
        TaotaoResult result = null;
        try {
            itemService.reshelfItemById(ids);
            result = TaotaoResult.ok();
        } catch (Exception e) {
            result = TaotaoResult.build(1, e.getMessage());
        }
        return result;
    }

    @RequestMapping(value = "/pic/upload", produces = MediaType.TEXT_PLAIN_VALUE + ";charset=utf-8")
    @ResponseBody
    public String upload(MultipartFile uploadFile) {
        Map<String, Object> result = new HashMap<>();
        try {
            String originalFilename = uploadFile.getOriginalFilename();
            String ext = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            byte[] bvs = uploadFile.getBytes();
            FastDFSClient client = new FastDFSClient("classpath:resource/fast_dfs.conf");
            String storageUrl = client.uploadFile(bvs, ext);
            String uploadedFile = TAOTAO_IMAGE_SERVER_URL + storageUrl;
            result.put("error", 0);
            result.put("url", uploadedFile);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("error", 1);
            result.put("message", "上传失败");
        }
        return JsonUtils.objectToJson(result);
    }

}
